package ar.com.ford.it.fullEconomics.view.form;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Collection;
import java.util.GregorianCalendar;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;
import org.apache.struts.validator.ValidatorForm;
import ar.com.ford.it.fullEconomics.persistence.dto.Teese03Econvalue;
import ar.com.ford.it.fullEconomics.struct.EconomicValueStruct;
import ar.com.ford.it.fullEconomics.util.FullEconomicsDateUtil;

@SuppressWarnings("serial")
public class EconomicValueForm extends ValidatorForm {

    String filterEstatus = null;
    String numberEconomicValue = null;
    String efectYear = null;
    String efectMonth = null;
    String sourcFind = null;
    String descFind = null;
    String typeFind = null;
    String codeFind = null;
    String codeEcon = null;
    String descEcon = null;
    String currEcon = null;
    Collection economicEfec = null;
    Collection economicSour = null;
    String section = null;
    String operation = null;
    String sourEco = null; // Identifica el sour del economic index ambos se
    // comparte en el edit como en el create
    String valEco = null;
    String porcEco = null; // Porcentaje de variacion
    String efecEco = null;
    String ecoStimateF = null;
    String typeCodeEco = null;
    String validateSubmit = "";
    Collection economicValues;
    Teese03Econvalue economicValue = null;
    Teese03Econvalue prevEconValue = null;

    public String getTypeCodeEco() {
        return typeCodeEco;
    }

    public void setTypeCodeEco(String typeCodeEco) {
        this.typeCodeEco = typeCodeEco;
    }

    public void resetForm() {

        this.filterEstatus = "";
        this.sourcFind = "";
        this.typeFind = "";
        this.codeFind = "";
        this.codeEcon = "";
        this.descEcon = "";
        this.currEcon = "";
        this.economicEfec = null;
        this.section = "";
        this.operation = "";
        this.sourEco = "";
        this.valEco = "";
        this.porcEco = "";
        this.efecEco = "";
        this.ecoStimateF = "";
        this.numberEconomicValue = "";
        this.prevEconValue = null;
    }

    public void resetPanel() {
        this.codeEcon = "";
        this.descEcon = "";
        this.currEcon = "";
        this.economicEfec = null;
        this.section = "";
        this.operation = null;
        this.sourEco = "";
        this.valEco = "";
        this.porcEco = "";
        this.efecEco = "";
        this.ecoStimateF = "";
    }

    public String getCodeEcon() {
        return codeEcon;
    }

    public void setCodeEcon(String codeEcon) {
        this.codeEcon = codeEcon;
    }

    public String getCodeFind() {
        return codeFind;
    }

    public void setCodeFind(String codeFind) {
        this.codeFind = codeFind;
    }

    public String getCurrEcon() {
        return currEcon;
    }

    public void setCurrEcon(String currEcon) {
        this.currEcon = currEcon;
    }

    public String getDescEcon() {
        return descEcon;
    }

    public void setDescEcon(String descEcon) {
        this.descEcon = descEcon;
    }

    public Collection getEconomicEfec() {
        return economicEfec;
    }

    public void setEconomicEfec(Collection economicEfec) {
        this.economicEfec = economicEfec;
    }

    public String getEfecEco() {
        return efecEco;
    }

    public void setEfecEco(String efecEco) {
        this.efecEco = efecEco;
    }

    public String getFilterEstatus() {
        return filterEstatus;
    }

    public void setFilterEstatus(String filterEstatus) {
        this.filterEstatus = filterEstatus;
    }

    public String getOperation() {
        return operation;
    }

    public void setOperation(String operation) {
        this.operation = operation;
    }

    public String getPorcEco() {
        return porcEco;
    }

    public void setPorcEco(String porcEco) {
        this.porcEco = porcEco;
    }

    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    public String getSourcFind() {
        return sourcFind;
    }

    public void setSourcFind(String sourcFind) {
        this.sourcFind = sourcFind;
    }

    public String getSourEco() {
        return sourEco;
    }

    public void setSourEco(String sourEco) {
        this.sourEco = sourEco;
    }

    public String getTypeFind() {
        return typeFind;
    }

    public void setTypeFind(String typeFind) {
        this.typeFind = typeFind;
    }

    public String getValEco() {
        return valEco;
    }

    public void setValEco(String valEco) {
        this.valEco = valEco;
    }

    public Collection getEconomicSour() {
        return economicSour;
    }

    public void setEconomicSour(Collection economicSour) {
        this.economicSour = economicSour;
    }

    public Collection getEconomicValues() {
        return economicValues;
    }

    public void setEconomicValues(Collection economicValues) {
        this.economicValues = economicValues;
    }

    public void economicValuesMapped(String key, String value) {
        List economicValue = (List) this.getEconomicValues();
        EconomicValueStruct ecoS = (EconomicValueStruct) economicValue
                .get(Integer.parseInt(key) - 1);
        this.setEconomicValue(ecoS.getEconomicValueActual());
        this.setPrevEconValue(ecoS.getEconomicValuePasado());
    }

    public void setEditMapped(String key, String value) {
        List economicValue = (List) this.getEconomicValues();
        EconomicValueStruct ecoS = (EconomicValueStruct) economicValue
                .get(Integer.parseInt(key) - 1);
        this.setEconomicValue(ecoS.getEconomicValueActual());
    }

    public void setDeletMapped(String key, String value) {
        List economicValue = (List) this.getEconomicValues();
        this.setEconomicValue((Teese03Econvalue) economicValue.get(Integer
                .parseInt(key) - 1));
    }

    public void setEditParametersPanel() {
        this.setCodeEcon(this.getEconomicValue().getTeese01Economic()
                .getEese01EconNumberK().toString());
        this.setCurrEcon(this.getEconomicValue().getEese03CurrencyCodeC()
                .getEesm01CurrencyCodeC());
        this.setDescEcon(this.getEconomicValue().getTeese01Economic()
                .getEese01EconNameN().toString());
        BigDecimal bd1 = this.getEconomicValue()
                .getEese03EcvalueVariaPercentP();
        if (bd1 != null) {
            BigDecimal bd1s2 = bd1.setScale(2, BigDecimal.ROUND_DOWN);
            this.setPorcEco(bd1s2.toString());
        }
        this.setSourEco(this.getEconomicValue().getTeese01Economic()
                .getEese01EconsourceNumberR().getEese02EconsourceNameN());
        BigDecimal bd2 = this.getEconomicValue().getEese03EcvalueAmountA();
        BigDecimal bd2s2 = bd2.setScale(2, BigDecimal.ROUND_DOWN);
        this.setValEco(bd2s2.toString());
        Calendar calFecha = Calendar.getInstance();
        calFecha.setTime(this.getEconomicValue()
                .getEese03EcvalueEffectivDateY());
        this.setEfecEco(FullEconomicsDateUtil.format(calFecha));
        // Seteo si el valor es estimado o no
        this.setEcoStimateF(this.getEconomicValue()
                .getEese03EcvalueEstimateFlagF());

    }

    public Teese03Econvalue getEconomicValue() {
        return economicValue;
    }

    public void setEconomicValue(Teese03Econvalue economicValue) {
        this.economicValue = economicValue;
    }

    public String getDescFind() {
        return descFind;
    }

    public void setDescFind(String descFind) {
        this.descFind = descFind;
    }

    public void setNewParametersPanel() {
        this.setCodeEcon(this.getEconomicValue().getTeese01Economic()
                .getEese01EconNumberK().toString());
        this.setCurrEcon(this.getEconomicValue().getEese03CurrencyCodeC()
                .getEesm01CurrencyCodeC());
        this.setDescEcon(this.getEconomicValue().getTeese01Economic()
                .getEese01EconNameN().toString());
        this.setPorcEco("");
        this.setSourEco(this.getEconomicValue().getTeese01Economic()
                .getEese01EconsourceNumberR().getEese02EconsourceNameN());
        this.setValEco("");
        this.setEfecEco("");
        // Seteo si el valor es estimado o no
        this.setEcoStimateF("");

    }

    public String getNumberEconomicValue() {
        return numberEconomicValue;
    }

    public void setNumberEconomicValue(String numberEconomicValue) {
        this.numberEconomicValue = numberEconomicValue;
    }

    public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {
        ActionErrors ae = new ActionErrors();
        MessageResources resources = (MessageResources) request
                .getAttribute(org.apache.struts.Globals.MESSAGES_KEY);
        if ("find".equals(this.section)) {
            if ((StringUtils.isEmpty(this.getCodeFind()))
                    && (StringUtils.isEmpty(this.getDescFind()))) {
            } else {

                if (!(this.getCodeFind().matches("[0-9]+"))
                        && (StringUtils.isNotBlank(this.getCodeFind()))) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "errors.integergtzero", resources
                                    .getMessage("economicSource.label.code")));
                }
                if (this.getDescFind().matches("[0-9]+")
                        && (StringUtils.isNotBlank(this.getDescFind()))) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "errors.integergtzero",
                                            resources
                                                    .getMessage("costStructure.itemCategory.label.name")));
                }

            }
            if (StringUtils.isEmpty(this.getEfectYear())) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "errors.required", resources.getMessage("label.year")));
            } else if (!this.getEfectYear().matches("[0-9]+")) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "errors.invalid", resources.getMessage("label.year")));
            }
        }

        if (getValidateSubmit().equals("submit")) {
            if ((getSection().equals("edit") || getSection().equals("new"))
                    && getEconomicValue() != null) {
                if ((this.getPrevEconValue() == null
                        || this.getPrevEconValue().getEese03EcvalueAmountA() == null)
                        && StringUtils.isBlank(this.getValEco())) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "errors.required",
                                            resources
                                                    .getMessage("economicValue.label.tableHeader.valor")));
                }
                if (!StringUtils.isBlank(this.getValEco())) {
                    try {
                        Double.valueOf(getValEco());
                    } catch (Exception e) {
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("economicValue.error.valA"));
                    }
                }
                try {
                    if (Double.valueOf(getPorcEco()) > 100)
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage(
                                        "economicValue.error.porcMenor"));
                } catch (Exception e) {
                }
                if (getCurrEcon().equals("-1")) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "economicValue.error.moneda"));
                }
                if (getEcoStimateF().equals("-1")) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "economicValue.error.estimateF"));
                }

                if (FullEconomicsDateUtil.format(getEfecEco()) == null) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "economicValue.error.fecha"));
                } else {
                    GregorianCalendar auxCa2 = new GregorianCalendar();
                    auxCa2.setTimeInMillis(System.currentTimeMillis());
                    if (FullEconomicsDateUtil.format(getEfecEco()).before(
                            auxCa2)) {
                        if (!getEcoStimateF().equals("0")) {
                            ae.add(ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "economicValue.error.noEstimado"));
                        }
                    } else {
                        GregorianCalendar auxCa = new GregorianCalendar();
                        auxCa.setTimeInMillis(System.currentTimeMillis());
                        if (FullEconomicsDateUtil.format(getEfecEco()).after(
                                auxCa)) {
                            if (!getEcoStimateF().equals("1")) {
                                ae
                                        .add(
                                                ActionMessages.GLOBAL_MESSAGE,
                                                new ActionMessage(
                                                        "economicValue.error.noEstimado"));
                            }
                        }

                    }
                }
                if ("edit".equalsIgnoreCase(this.getSection())) {
                    if (!StringUtils.isEmpty(getValEco())
                            && !StringUtils.isEmpty(getPorcEco())) {
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage(
                                        "economicValue.error.ambiguos"));
                    }
                }

                /*
                 * if (getSection().equals("new") && getEconomicValue() != null) {
                 * GregorianCalendar auxCa2 = new GregorianCalendar();
                 * auxCa2.setTimeInMillis(System.currentTimeMillis()); if
                 * (FullEconomicsDateUtil.format(getEfecEco()).before( auxCa2)) {
                 * if (!getEcoStimateF().equals("0")) {
                 * ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                 * "economicValue.error.noEstimado")); } if
                 * (!this.getTypeCodeEco().equals("A")) {
                 * ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                 * "economicValue.error.futuro")); } } else { GregorianCalendar
                 * auxCa = new GregorianCalendar();
                 * auxCa.setTimeInMillis(System.currentTimeMillis()); if
                 * (FullEconomicsDateUtil.format(getEfecEco()).after( auxCa)) {
                 * if (!getEcoStimateF().equals("1")) { ae .add(
                 * ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                 * "economicValue.error.noEstimado")); } if
                 * (!this.getTypeCodeEco().equals("F")) {
                 * ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                 * "economicValue.error.futuro")); } } } }
                 */// comento porque es posible ingresar economic values
                // actuales en el pasado.
            }
        }
        return ae;
    }

    public String getValidateSubmit() {
        return validateSubmit;
    }

    public void setValidateSubmit(String validateSubmit) {
        this.validateSubmit = validateSubmit;
    }

    public String getEcoStimateF() {
        return ecoStimateF;
    }

    public void setEcoStimateF(String ecoStimateF) {
        this.ecoStimateF = ecoStimateF;
    }

    public String getEfectMonth() {
        return efectMonth;
    }

    public void setEfectMonth(String efectMonth) {
        this.efectMonth = efectMonth;
    }

    public String getEfectYear() {
        return efectYear;
    }

    public void setEfectYear(String efectYear) {
        this.efectYear = efectYear;
    }

    public Teese03Econvalue getPrevEconValue() {
        return prevEconValue;
    }

    public void setPrevEconValue(Teese03Econvalue prevEconValue) {
        this.prevEconValue = prevEconValue;
    }
}
